﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LoongCAD.STC.Friendly.Scene;
using LoongCAD.STC.Friendly.Dialog;
using LoongCAD.Geometry.Friendly;
using LoongCAD.STC.Friendly.Catalogs;
using LoongCAD.STC.Friendly;
using System.Diagnostics;
using LoongCAD.STC.Friendly.Plugins;
using LoongCAD.STC.Friendly.Scene.UI;


namespace HelloUtilities.CreateRandomColumns {
    /// <summary>
    /// 应用插件UI界面应派生自PluginFormBase
    /// </summary>
    public partial class HelloUtilityForm : PluginFormBase {
        private static HelloUtilityForm m_openedForm = null;
        private HelloUtilityData m_data = new HelloUtilityData();

        public HelloUtilityForm() {
            InitializeComponent();

            this.InitializeForm(); //初始化窗口，创建窗口后必须执行此函数
        }

        //创建并显示UI
        public static void CreateAndShowForm() {
            if (m_openedForm != null) { //若窗口已经打开，则不再开启新窗口
                m_openedForm.Focus();
                
                if (m_openedForm.WindowState == FormWindowState.Minimized) //若窗口当前最小化，则恢复显示
                    m_openedForm.WindowState = FormWindowState.Normal;

                return;
            }

            //当前没有打开窗口，新建窗口实例
            m_openedForm = new HelloUtilityForm();
            m_openedForm.ShowForm();
        }

        //UI关闭时触发
        protected override void OnFormClosing(FormClosingEventArgs e) {
            m_openedForm = null; //关闭窗口时，将m_openedForm置空
            base.OnFormClosing(e);
        }

        //点击按钮"创建柱"
        private void btn_create_Click(object sender, EventArgs e) {
            //打开全部筛选CheckBox
            this.SetDialogToggles(true);

            //获取UI输入并保存于m_data
            this.Modify(m_data);

            //仅独立程序远程调用形式与组件的DefineInput可在主线程使用Picker对象
            //其它情况，需要使用AsyncPicker
            var picker = new AsyncPicker();
            for (int i = 0; i < m_data.ColumnCount; ++i)
                picker.PickPoint($"捕捉第{i}点");

            //注册单次捕捉回调函数
            picker.PickedOneInput += this.PickedOneInput;
            //注册全部捕捉完成回调函数
            picker.PickedDone += this.PickedDone;
            //注册捕捉中断回调函数
            picker.PickingInterrupted += this.PickingInterrupted;

            //发起用户捕捉操作
            picker.DoPick();
        }

        //AsyncPicker.DoPick每做完一次捕捉的回调函数
        private bool PickedOneInput(PluginBase.InputDefinition input, int inputIdx) {
            //获取输入
            var pt = (Point)input.GetInput();

            //创建1根柱子
            var beam = new Beam();
            beam.StartPoint = new Point(pt.X, pt.Y, -200);
            beam.EndPoint = new Point(pt.X, pt.Y, 7200);
            beam.Profile.ProfileString = m_data.Profile;
            beam.Material.MaterialString = m_data.Material;
            beam.Insert();

            //继续捕捉下一点
            return true; 
        }

        //AsyncPicker.DoPick全部完成的回调函数
        private void PickedDone(List<PluginBase.InputDefinition> inputs) {
            MessageBox.Show("创建完成");
        }

        //AsyncPicker.DoPick异常中断回调函数
        private void PickingInterrupted(List<PluginBase.InputDefinition> inputs, bool userInterrupt) {
            if (userInterrupt)
                MessageBox.Show("用户中断");
            else
                MessageBox.Show("程序中断");
        }

        //点击截面型材选择框
        private void btn_profile_SelectionDone(object sender, EventArgs e) {
            tb_profile.Text = btn_profile.SelectedProfile;
        }

        //点击材质选择框
        private void btn_material_SelectionDone(object sender, EventArgs e) {
            tb_material.Text = btn_material.SelectedMaterial;
        }
    }
}
